<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品详情 - 简单商城</title>
    <link rel="stylesheet" href="css/styles.css">
    <style>
        /* 用户信息样式 */
        header {
            position: relative;
        }
        #userInfo {
            position: absolute;
            right: 1rem;
            top: 50%;
            transform: translateY(-50%);
            display: flex;
            align-items: center;
            gap: 1rem;
        }
        #userInfo button {
            background: #e74c3c;
            color: white;
            border: none;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        #userInfo button:hover {
            background: #c0392b;
        }

        /* 详情页特定样式 */
        .product-detail {
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            padding: 2rem;
            max-width: 1000px;
            margin: 0 auto;
        }

        .product-detail img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin-bottom: 2rem;
        }

        .product-detail h2 {
            color: #2c3e50;
            font-size: 2rem;
            margin-bottom: 1rem;
        }

        .product-detail .price {
            font-size: 1.5rem;
            color: #e74c3c;
            font-weight: bold;
            margin-bottom: 1rem;
        }

        .product-detail .details {
            color: #666;
            line-height: 1.8;
            margin-bottom: 2rem;
        }

        .back-link {
            display: inline-block;
            margin-bottom: 1rem;
            color: #3498db;
            text-decoration: none;
            font-weight: bold;
        }

        .back-link:hover {
            text-decoration: underline;
        }

        /* 评论区样式 */
        .comments-section {
            margin-top: 3rem;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            padding: 2rem;
        }

        .comment-form {
            margin-bottom: 2rem;
        }

        .comment-form textarea {
            width: 100%;
            padding: 1rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 1rem;
            min-height: 100px;
            resize: vertical;
        }

        .form-row {
            display: flex;
            gap: 1rem;
            align-items: center;
            margin-bottom: 1rem;
        }

        .rating-select {
            padding: 0.5rem;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .submit-comment {
            background-color: #3498db;
            color: white;
            padding: 0.8rem 1.5rem;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .submit-comment:hover {
            background-color: #2980b9;
        }

        .comments-list {
            margin-top: 2rem;
        }

        .comment {
            border-bottom: 1px solid #eee;
            padding: 1rem 0;
        }

        .comment:last-child {
            border-bottom: none;
        }

        .comment-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 0.5rem;
        }

        .comment-user {
            font-weight: bold;
            color: #2c3e50;
        }

        .comment-rating {
            color: #f1c40f;
            letter-spacing: 2px;
        }

        .comment-content {
            color: #333;
            line-height: 1.6;
            margin-bottom: 0.5rem;
        }

        .comment-footer {
            color: #666;
            font-size: 0.9rem;
        }

        .no-comments, .loading, .error {
            text-align: center;
            padding: 2rem;
            font-style: italic;
        }

        .no-comments {
            color: #666;
        }

        .loading {
            color: #3498db;
        }

        .error {
            color: #e74c3c;
        }

        .submit-comment:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
        }

        .submit-feedback {
            margin-top: 1rem;
            padding: 0.8rem;
            border-radius: 4px;
            text-align: center;
        }

        .submit-feedback.success {
            background-color: #2ecc71;
            color: white;
        }

        .submit-feedback.error {
            background-color: #e74c3c;
            color: white;
        }

        .retry-button {
            margin-top: 1rem;
            padding: 0.5rem 1rem;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .retry-button:hover {
            background-color: #2980b9;
        }

        .error {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 1rem;
        }

        @media (max-width: 768px) {
            .product-detail {
                padding: 1rem;
            }

            .product-detail h2 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1>简单商城</h1>
    </header>
    
    <main>
        <div class="product-detail" id="productDetail">
            <a href="index.html" class="back-link">← 返回商品列表</a>
            <!-- 商品详情将通过JavaScript动态加载 -->
        </div>
    </main>

    <footer>
        <p>&copy; 2024 简单商城</p>
    </footer>

    <script src="js/auth.js"></script>
    <script src="js/products.js"></script>
    <script src="js/comments.js"></script>
    <script>
        // 渲染商品详情并初始化评论
        async function initPage() {
            await renderProductDetail();
            await initComments();
        }

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', initPage);

        // 渲染商品详情
        function renderProductDetail() {
            const productId = parseInt(getUrlParam('id'));
            const product = products.find(p => p.id === productId);
            
            if (!product) {
                window.location.href = 'index.html';
                return;
            }

            const container = document.getElementById('productDetail');
            // 清空容器
            container.innerHTML = '';
            
            // 添加商品信息
            const productInfo = document.createElement('div');
            productInfo.innerHTML = `
                <img src="${product.image}" alt="${product.name}" onerror="this.src='images/placeholder.jpg'">
                <h2>${product.name}</h2>
                <p class="price">￥${product.price.toFixed(2)}</p>
                <div class="details">
                    <p>${product.details}</p>
                </div>
            `;
            container.appendChild(productInfo);

            // 添加评论区域
            const commentsSection = document.createElement('div');
            commentsSection.className = 'comments-section';
            commentsSection.innerHTML = `
                <h3>商品评价</h3>
                <form id="commentForm" class="comment-form">
                    <textarea id="commentContent" placeholder="请输入您的评价..." required></textarea>
                    <div class="form-row">
                        <select id="rating" class="rating-select">
                            <option value="5">5星 - 非常满意</option>
                            <option value="4">4星 - 满意</option>
                            <option value="3">3星 - 一般</option>
                            <option value="2">2星 - 不满意</option>
                            <option value="1">1星 - 非常不满意</option>
                        </select>
                        <button type="submit" class="submit-comment">提交评价</button>
                    </div>
                </form>
                <div id="commentsList" class="comments-list">
                    <!-- 评论将通过JavaScript动态加载 -->
                </div>
            `;
            container.appendChild(commentsSection);

            document.title = `${product.name} - 简单商城`;
        }

        document.addEventListener('DOMContentLoaded', renderProductDetail);
    </script>
</body>
</html>